package cn.wzvtc.rbac_mp.controller;

import cn.wzvtc.common.dto.Pagination;
import cn.wzvtc.common.dto.Result;
import cn.wzvtc.rbac_mp.entity.Menu;
import cn.wzvtc.rbac_mp.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Shiwei Xu
 * @date 2020/5/27
 */
@RestController
@RequestMapping("menu")
public class MenuController {

  @Autowired
  private MenuService menuService;

  /**
   * 查询的思路
   * @param name
   * @param pagination
   */
  @GetMapping
  public Result<IPage<Menu>> list(String name, Pagination pagination) {
    // select * from menu where name like '%%'
    QueryWrapper<Menu> wrapper = new QueryWrapper<>();
    wrapper.like(!StringUtils.isEmpty(name), "menu_name", name);

    IPage<Menu> page = pagination.toIPage();

    IPage<Menu> page1 = menuService.page(page, wrapper);
    return Result.ok(page1);

  }


  @PostMapping
  public Result<Void> add(@RequestBody Menu menu) {
    menuService.save(menu);
    return Result.ok();
  }

  @PutMapping
  public Result<Void> update(@RequestBody Menu menu) {
    menuService.updateById(menu);
    return Result.ok();
  }

  @DeleteMapping
  public Result<Void> delete(List<Long> ids) {
    menuService.removeByIds(ids);
    return Result.ok();
  }


}
